/**
 * 
 */
package com.geezdata.cps.ttk.core.command;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.response.BaseResponse;
import com.geezdata.cps.base.utils.BeanUtil;
import com.geezdata.cps.cqrs.command.Command;
import com.geezdata.cps.cqrs.command.bus.CommandBus;
import com.geezdata.cps.ttk.common.request.CreateOrUpdateGoodsRequest;
import com.geezdata.cps.ttk.common.request.CreateOrUpdateTaskRequest;
import com.geezdata.cps.ttk.common.response.CreateOrUpdateGoodsResult;
import com.geezdata.cps.ttk.common.response.CreateOrUpdateTaskResult;
import com.geezdata.cps.ttk.common.vo.GoodsReqVO;
import com.geezdata.cps.ttk.common.vo.PinduoduoDDKGoodsDetailResVO;
import com.geezdata.cps.ttk.common.vo.TaskReqVO;
import com.geezdata.cps.ttk.domain.domain.Task;
import com.geezdata.cps.ttk.domain.factory.TaskFactory;
import com.geezdata.cps.ttk.integration.ddk.DdkIntegrationService;
import com.geezdata.cps.ttk.mapper.mapper.TaskMapper;
import com.geezdata.cps.ttk.query.query.SharerService;

/**
 * @author zheng
 *
 */
@Service
public class CreateOrUpdateTaskCommand implements Command<CreateOrUpdateTaskRequest, CreateOrUpdateTaskResult>{
	@Autowired
	private DdkIntegrationService ddkIntegrationService;
	@Autowired
	private CommandBus commandBus;
	@Autowired
	private Command<CreateOrUpdateGoodsRequest, CreateOrUpdateGoodsResult> createOrUpdateGoodsCommand;
	@Autowired
	private TaskMapper taskMapper;
	@Autowired
	private SharerService sharerService;
	
	@Override
	@Transactional
	public BaseResponse<CreateOrUpdateTaskResult> execute(CreateOrUpdateTaskRequest request) {
		TaskReqVO taskReqVO = request.getTask();
		List<PinduoduoDDKGoodsDetailResVO> goodsList = ddkIntegrationService.getDDKGoodsDetails(taskReqVO.getGoodsId());
		if(CollectionUtils.isEmpty(goodsList)) {
			return BaseResponse.error(ErrorCode.INVALID_DDK_GOODS_ID.getCode(), ErrorCode.INVALID_DDK_GOODS_ID.getDesc());
		}
		
		List<PinduoduoDDKGoodsDetailResVO> filterList = goodsList.stream().filter(goodsDetailVO -> isMatch(goodsDetailVO, taskReqVO.getGoodsId())).collect(Collectors.toList());
		if(CollectionUtils.isEmpty(filterList) || filterList.size() >= 2) {
			return BaseResponse.error(ErrorCode.INVALID_DDK_GOODS_ID.getCode(), ErrorCode.INVALID_DDK_GOODS_ID.getDesc());
		}
		
		sharerService.getSharerByOpenId(taskReqVO.getOpenId());
		
		createOrUpdateGoods(filterList.get(0));
		
		return createOrUpdateTask(request);
	}

	private BaseResponse<CreateOrUpdateTaskResult> createOrUpdateTask(CreateOrUpdateTaskRequest request) {
		CreateOrUpdateTaskResult result = new CreateOrUpdateTaskResult();
		
		Task exsitsTask = testIfExistTask(request);
		Task task = TaskFactory.newInstance(request, exsitsTask);
		createOrUpdateTask(exsitsTask, task);
        result.setTaskId(task.getTaskId());
		
		return BaseResponse.success(result);
	}

	private void createOrUpdateTask(Task exsitsTask, Task task) {
		if(exsitsTask == null) {
			taskMapper.insert(task);
		} else {
			taskMapper.updateByPrimaryKey(task);
		}
	}

	private Task testIfExistTask(CreateOrUpdateTaskRequest request) {
		return null;
	}

	private void createOrUpdateGoods(PinduoduoDDKGoodsDetailResVO pinduoduoDDKGoodsDetailResVO) {
		CreateOrUpdateGoodsRequest createOrUpdateGoodsRequest = new CreateOrUpdateGoodsRequest();
		GoodsReqVO goodsReqVO = BeanUtil.convert(pinduoduoDDKGoodsDetailResVO, GoodsReqVO.class);
		goodsReqVO.setPddGoodsId(pinduoduoDDKGoodsDetailResVO.getGoodsId());
		goodsReqVO.setGoodsId(null);
		goodsReqVO.setHasCoupon(pinduoduoDDKGoodsDetailResVO.isHasCoupon()?1:0);
		createOrUpdateGoodsRequest.setGoodsReqVO(goodsReqVO);
		commandBus.dispatch(createOrUpdateGoodsCommand, createOrUpdateGoodsRequest);
	}

	private Boolean isMatch(PinduoduoDDKGoodsDetailResVO goodsDetailVO, String goodsId) {
		return goodsDetailVO.getGoodsId().equals(goodsId);
	}
}
